#include "Interactiv/InteractivPublic.h"

Axis::Axis(osg::ref_ptr<ChaiDanManipulator> manipulator, osg::BoundingSphere boundsphere, RECT rect)
{
	m_bimorbitmanipulator = manipulator;
	m_boundsphere_ = boundsphere;
	rect_ = rect;
}

Axis::~Axis()
{

}

bool Axis::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)
{
	osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
	if (viewer == nullptr)
	{
		return false;
	}

	switch (ea.getEventType())
    {
		case(osgGA::GUIEventAdapter::FRAME):  
		{
			if (m_bimorbitmanipulator && m_pHudCamera)
			{
				osg::Vec3d eye, up, center;
				m_bimorbitmanipulator->getTransformation(eye, center, up);
				osg::Vec3 delt = center - eye;
				delt.normalize();

				m_pHudCamera->setViewMatrixAsLookAt(
					delt * (-1000.0) * m_cubeWidth,
					osg::Vec3(0, 0, 0),
					up);
			}
			return false;
		}

    default:
        return false;
    }
}

void Axis::setHudViewPositionAndCubeWidth(double _x, double _y, double _width, double _height, float _cubeWidth)
{
	m_pViewport = new osg::Viewport(-20, 20, _width * 0.1, _height * 0.1);
	m_cubeWidth = _cubeWidth;
}

osg::ref_ptr<osg::Camera> Axis::Creat3DCamera(osg::ref_ptr<osg::Camera> hudcamera)
{
	m_pHudCamera = hudcamera;

	m_pHudCamera->setViewMatrix ( osg::Matrix ::identity ());
	m_pHudCamera->setRenderOrder(osg::Camera::POST_RENDER);
	m_pHudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
	m_pHudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);

	osg::ref_ptr<osg::Geode> geode = OsgExTool::OsgExToolSet::CreateAxis(400, 15, 50, 40);


	m_pHudCamera->addChild(geode);

	m_pHudCamera->setViewMatrixAsLookAt(
		osg::Vec3(0, 0, 1000 * m_cubeWidth),
		osg::Vec3(0, 0, 0), 
		osg::Vec3(0, 1, 0)
		);

	m_pHudCamera->setViewport(m_pViewport);

	m_pHudCamera->setName("hudcamera");

	return m_pHudCamera; 
}




